package com.java110.mall.chain.adapt.order;

import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.factory.SendSmsFactory;
import com.java110.dto.data.DatabusDataDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.shopSms.ShopSmsDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrderWriteoff.StoreOrderWriteoffDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderWriteoffV1InnerServiceSMO;
import com.java110.intf.chain.IPrintOrderV1InnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.shop.IShopSmsDetailV1InnerServiceSMO;
import com.java110.intf.shop.IShopSmsV1InnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.chain.adapt.IDatabusAdapt;
import com.java110.po.shopSms.ShopSmsPo;
import com.java110.po.shopSmsDetail.ShopSmsDetailPo;
import com.java110.utils.lock.DistributedLock;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 完成备货
 * 发送短信
 * 打印 核销单
 */
@Service("finishReadyOrderDataBus")
public class FinishReadyOrderDataBus implements IDatabusAdapt {

    @Autowired
    private IStoreOrderWriteoffV1InnerServiceSMO storeOrderWriteoffV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Autowired
    private IShopSmsV1InnerServiceSMO shopSmsV1InnerServiceSMOImpl;

    @Autowired
    private IShopSmsDetailV1InnerServiceSMO shopSmsDetailV1InnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IPrintOrderV1InnerServiceSMO printOrderV1InnerServiceSMOImpl;

    @Override
    public void execute(DatabusDataDto databusDataDto) throws Exception {

        String orderId = databusDataDto.getBusinessId();

        StoreOrderWriteoffDto storeOrderWriteoffDto = new StoreOrderWriteoffDto();
        storeOrderWriteoffDto.setOrderId(orderId);
        List<StoreOrderWriteoffDto> storeOrderWriteoffDtos
                = storeOrderWriteoffV1InnerServiceSMOImpl.queryStoreOrderWriteoffs(storeOrderWriteoffDto);
        if (ListUtil.isNull(storeOrderWriteoffDtos)) {
            return;
        }

        StoreOrderDto storeOrderDto = new StoreOrderDto();
        storeOrderDto.setOrderId(orderId);
        List<StoreOrderDto> storeOrderDtos = storeOrderInnerServiceSMOImpl.queryStoreOrders(storeOrderDto);
        if (ListUtil.isNull(storeOrderDtos)) {
            return;
        }

        // todo 给用户发送短信
        sendReadyMsg(storeOrderDtos.get(0), storeOrderWriteoffDtos.get(0));

        // todo 打印核销单
        printOrderV1InnerServiceSMOImpl.printReadyStoreOrder(storeOrderWriteoffDtos.get(0));

    }

    private void sendReadyMsg(StoreOrderDto storeOrderDto, StoreOrderWriteoffDto storeOrderWriteoffDto) {

        ShopSmsDto shopSmsDto = new ShopSmsDto();
        shopSmsDto.setShopId(storeOrderWriteoffDto.getShopId());
        List<ShopSmsDto> shopSmsDtos = shopSmsV1InnerServiceSMOImpl.queryShopSmss(shopSmsDto);
        if (ListUtil.isNull(shopSmsDtos)) {
            return;
        }
        int smsCount = Integer.parseInt(shopSmsDtos.get(0).getSmsCount());
        if (smsCount < 1) {
            return;
        }


        String personId = storeOrderDto.getPersonId();
        UserDto userDto = new UserDto();
        userDto.setUserId(personId);
        List<UserDto> personUserDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
        if (ListUtil.isNull(personUserDtos)) {
            return;
        }
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(storeOrderWriteoffDto.getShopId());
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        if (ListUtil.isNull(shopDtos)) {
            throw new IllegalArgumentException("店铺不存在");

        }
        String content = "您购买的商品已经准备好，请凭" + storeOrderWriteoffDto.getWoNum() + "到店取件。";
        SendSmsFactory.sendReadyOrderMsg(personUserDtos.get(0).getTel(), storeOrderWriteoffDto.getWoNum());
        String requestId = DistributedLock.getLockUUID();
        String key = this.getClass().getSimpleName() + storeOrderWriteoffDto.getShopId();

        try {
            //开启事务
            DistributedLock.waitGetDistributedLock(key, requestId);
            ShopSmsDto tmpShopSmsDto = new ShopSmsDto();
            tmpShopSmsDto.setSmsId(shopSmsDto.getSmsId());
            shopSmsDtos = shopSmsV1InnerServiceSMOImpl.queryShopSmss(tmpShopSmsDto);

            ShopSmsPo shopSmsPo = new ShopSmsPo();
            smsCount = Integer.parseInt(shopSmsDtos.get(0).getSmsCount());
            smsCount -= 1;
            if (smsCount < 1) {
                smsCount = 0;
            }
            shopSmsPo.setSmsCount(smsCount + "");
            shopSmsPo.setSmsId(shopSmsDto.getSmsId());
            shopSmsV1InnerServiceSMOImpl.updateShopSms(shopSmsPo);

            ShopSmsDetailPo shopSmsDetailPo = new ShopSmsDetailPo();
            shopSmsDetailPo.setSmsId(shopSmsDtos.get(0).getSmsId());
            shopSmsDetailPo.setSmsContent(content);
            shopSmsDetailPo.setLink(personUserDtos.get(0).getTel());
            shopSmsDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId("11"));
            shopSmsDetailPo.setShopName(shopDtos.get(0).getShopName());
            shopSmsDetailPo.setShopId(shopDtos.get(0).getShopId());
            shopSmsDetailV1InnerServiceSMOImpl.saveShopSmsDetail(shopSmsDetailPo);

        } finally {
            //清理事务信息
            DistributedLock.releaseDistributedLock(key, requestId);
        }
    }
}
